# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render,redirect,reverse
from .models import Client,Personnel,Order_form
from .froms import Client_rechargeFrom,OrderFrom
from django.core.paginator import Paginator
# Create your views here.
def order_forms(request,userid):
    if request.method == 'GET':
        user = Client.objects.get(id=userid)
        return render(request, 'order/oder_forms.html', {'user': user})
    else:
        print "*********************************"
        client_username = request.POST.get('client_username', None)
        havehaircut_username = request.POST.get('havehaircut_username', None)
        washhair_username = request.POST.get('washhair_username', None)
        order_money = request.POST.get('order_money', None)
        washhair = Personnel.objects.filter(username=washhair_username).first()
        havehaircut = Personnel.objects.filter(username=havehaircut_username).first()
        if not washhair and '0' not in washhair.personnel_type.personneltype:
            return render(request, 'order/oder_forms.html', {'error': u'没有这个洗发师'})
        if not havehaircut and '1' not in havehaircut.personnel_type.personneltype:
            return render(request, 'order/oder_forms.html', {'error': u'没有这个理发师'})
        client_user = Client.objects.filter(client_username=client_username).first()
        if client_user:
            print '666666666666666666'
            client_user.money = client_user.money - int(order_money)
            client_user.save()
            order = Order_form()
            order.order_washhair_name = washhair
            order.order_havehaircut_name = havehaircut
            order.order_client_name = client_user
            order.order_money = int(order_money)
            order.save()
            return redirect('/results/index')

        else:
            return render(request, 'order/oder_forms.html', {'error': u'无此用户'})


def recharge(request, userid):
    if request.method == 'GET':
        user = Client.objects.get(id=userid)
        return render(request, 'order/recharge.html', {'user': user})
    else:

        username = request.POST.get('username', None)
        money = request.POST.get('money', None)
        user = Client.objects.filter(client_username=username).first()
        print user
        if user:
            user.money += int(money)
            user.save()
            return redirect('/account/cms_user_home/{}'.format(user.id))
        else:
            return render(request, 'account/modify_userhome.html', {'error': u'没有此用户'})


def orders(request):
    if request.method == 'GET':
        order_list = Order_form.objects.order_by('-times')
        curr_page = int(request.GET.get('curr_page', 1))
        # 使用django Paginator 进行分页， 这里页面展示5个
        p = Paginator(order_list, 5)
        page = p.page(curr_page)

        clients = Client.objects.all()
        personnel = Personnel.objects.all()
        return render(request, 'order/order_list.html', locals())



def order_sort(request):
    if request.method == 'GET':
        sorting = {
            '1': Client,
            '2': Personnel,
        }
        clients = Client.objects.all()
        personnel = Personnel.objects.all()
        userid = request.GET.get('userid', '1')
        print userid
        curr_page = int(request.GET.get('curr_page', 1))
        user_type = request.GET.get('user_type', '1')
        cli = sorting[user_type]
        user = cli.objects.get(id=userid)
        if user_type == '1':
            orders_list = user.order_name.order_by('-times').all()
        else:
            if user.personnel_user__personneltype == '0':
                orders_list = user.havehaircut_name.order_by('-times')
            else:
                orders_list = user.washhair_name.order_by('-times')

        # 使用django Paginator 进行分页， 这里页面展示5个
        p = Paginator(orders_list, 5)
        page = p.page(curr_page)
        return render(request, 'order/order_list.html', locals())


